﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using NewDDSys.Entity;
using NewDDSys.IBLL;
using NewDDSys.IDAL;
using NewDDSys.Factory;
using NewDDSys.Utility;
using AjaxControlToolkit;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Web_KFZW : System.Web.UI.Page
{
    private string num = "";
    private string Nsta = "";
    private int good = 0;
    private int bad = 0;
    private int cc = 0;
    private int pb = 0;
    private int qh = 0;
    private string ku = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        num = Request.QueryString["Num"];
        DataSet ds = BLLFactory.CreateWorkBLL().Select(num);
        
        if (ds == null)
            Response.Write(" <script   language=javascript> window.alert( '无此记录，请联系管理员！ ');</script> ");
        else
        {
            txtNUM.Text = num;
            txtNUM.Attributes["contentEditable"] = "false";
            Nsta = ds.Tables[0].Rows[0]["sta"].ToString();
            txtBanbie.Text = ds.Tables[0].Rows[0]["banbie"].ToString();
            
            txtBanzu.Text = ds.Tables[0].Rows[0]["banzu"].ToString();
            
            txtTianbao.Text = ds.Tables[0].Rows[0]["tianbao"].ToString();
            
            txtKu.Text = ds.Tables[0].Rows[0]["ku"].ToString();
            ku = txtKu.Text.ToString();
            txtDun.Text = ds.Tables[0].Rows[0]["dun"].ToString();
            
            txtNeirong.Text = ds.Tables[0].Rows[0]["neirong"].ToString();
            
            txtPos.Text = ds.Tables[0].Rows[0]["pos"].ToString();
            
            txtPDtime.Text = ds.Tables[0].Rows[0]["pdtime"].ToString();
            
            
            
            txtGood.Text = ds.Tables[0].Rows[0]["good"].ToString();
            good = Convert.ToInt32(txtGood.Text.ToString());
            
            txtBad.Text = ds.Tables[0].Rows[0]["bad"].ToString();
            
            txtPb.Text = ds.Tables[0].Rows[0]["pb"].ToString();
            pb = Convert.ToInt32(txtPb.Text.ToString());
            txtCc.Text = ds.Tables[0].Rows[0]["cc"].ToString();
            cc = Convert.ToInt32(txtCc.Text.ToString());
            txtQH.Text = ds.Tables[0].Rows[0]["qh"].ToString();
            qh = Convert.ToInt32(txtQH.Text.ToString());
            if (Session["用户信息.部门"].ToString() != "一高线")
                txtQH.Attributes["contentEditable"] = "false";
            txtKx.Text = ds.Tables[0].Rows[0]["kx"].ToString();
            txtKx.Attributes["contentEditable"] = "false";
            if (Nsta == "未装完")
            {
                txtWcs.Text = txtGood.Text.ToString();
                txtWcs.Attributes["contentEditable"] = "false";
                txtBanbie.Attributes["contentEditable"] = "false";
                txtBanzu.Attributes["contentEditable"] = "false";
                txtTianbao.Attributes["contentEditable"] = "false";
                txtKu.Attributes["contentEditable"] = "false";
                txtDun.Attributes["contentEditable"] = "false";
                txtNeirong.Attributes["contentEditable"] = "false";
                txtPos.Attributes["contentEditable"] = "false";
                txtPDtime.Attributes["contentEditable"] = "false";
                txtGood.Attributes["contentEditable"] = "false";
                txtBad.Attributes["contentEditable"] = "false";
                txtPb.Attributes["contentEditable"] = "false";
                txtCc.Attributes["contentEditable"] = "false";
                txtQH.Attributes["contentEditable"] = "false";
            }
        }
        
    }
    protected void btnOK_Click(object sender, EventArgs e)
    {
        
        
        
        int wc = 0;
        if (txtWcs.Text.ToString() != "")
            wc = Convert.ToInt32(txtWcs.Text.ToString());
        if (wc > good)
                Response.Write(" <script   language=javascript> window.alert( ' 完成车数填写不正确！ ');</script> ");
        else
        {
            if (wc == good)
            {
                string zwtime = DateTime.Now.ToString();
                int f = BLLFactory.CreateWorkBLL().UpdateZw(num, zwtime);
                DataSet ds = BLLFactory.CreateDxcBLL().SelectNum(num);
                if (ds != null)
                {
                    BLLFactory.CreateDxcBLL().UpdateCL(num, "已处理", zwtime, Session["用户信息.班组"].ToString());
                }
                
                if (f >= 1)
                {
                    Response.Write(" <script   language=javascript> window.alert( ' 已装完！ ');</script> ");
                }
                TimeSpan ts = Convert.ToDateTime(zwtime) - Convert.ToDateTime(txtPDtime.Text.ToString());
                decimal zc = Convert.ToDecimal(ts.TotalHours.ToString("f"));

                string sta = "装完";
                BLLFactory.CreateWorkBLL().UpdateSta(num, sta);
                BLLFactory.CreateWorkBLL().UpdateWcs(num, good);
                BLLFactory.CreateWorkBLL().UpdateZcys(num, zc);
                if (txtNeirong.Text.ToString() == "零提")
                {
                    if (Session["用户信息.部门"].ToString() == "嘉东库")
                    {
                        decimal yst = zc - 5 * good;
                        BLLFactory.CreateWorkBLL().UpdateYs(num, yst);
                    }
                    else
                    {
                        decimal yst = zc - 6 * good;
                        BLLFactory.CreateWorkBLL().UpdateYs(num, yst);
                    }

                }
                else
                {
                    int cs = good;
                    switch (ku)
                    {
                        case "板材库":
                            {
                                if (cs <= 6)
                                {
                                    decimal yst = zc - Convert.ToDecimal(0.5 * cs);
                                    BLLFactory.CreateWorkBLL().UpdateYs(num, yst);
                                }
                                else
                                {
                                    decimal yst = zc - 4;
                                    BLLFactory.CreateWorkBLL().UpdateYs(num, yst);
                                }
                            }; break;

                        case "一高线":
                            {
                                decimal yst = zc - Convert.ToDecimal(0.5 * cc + pb + 0.75 * qh);
                                BLLFactory.CreateWorkBLL().UpdateYs(num, yst);
                            }; break;
                        case "大棒线":
                            {
                                decimal yst = zc - Convert.ToDecimal(0.5 * cc + 0.83 * pb);
                                BLLFactory.CreateWorkBLL().UpdateYs(num, yst);
                            }; break;
                        case "嘉东线材":
                            {
                                decimal yst = zc - Convert.ToDecimal(0.5 * cs);
                                BLLFactory.CreateWorkBLL().UpdateYs(num, yst);
                            }; break;
                        case "嘉东螺纹":
                            {
                                decimal yst = zc - Convert.ToDecimal(0.75 * cc + pb);
                                BLLFactory.CreateWorkBLL().UpdateYs(num, yst);
                            }; break;
                        
                        case "嘉东板材":
                            {
                                if (cs <= 6)
                                {
                                    decimal yst = zc - Convert.ToDecimal(0.5 * cs);
                                    BLLFactory.CreateWorkBLL().UpdateYs(num, yst);
                                }
                                else
                                {
                                    decimal yst = zc - 4;
                                    BLLFactory.CreateWorkBLL().UpdateYs(num, yst);
                                }
                            }; break;
                        case "二高线":
                            {
                                decimal yst = zc - Convert.ToDecimal(0.5 * cs);
                                BLLFactory.CreateWorkBLL().UpdateYs(num, yst);
                            }; break;
                        case "四轧库":
                        case "化产库":
                            {
                                decimal yst = zc - Convert.ToDecimal(0.66 * cs);
                                BLLFactory.CreateWorkBLL().UpdateYs(num, yst);
                            }; break;
                        default:
                            break;

                    }
                    
                }
                Response.Redirect(string.Format("WORK.aspx?Sta={0}", sta));

            }

            else
            {
                BLLFactory.CreateWorkBLL().UpdateWcs(num, wc);
                string sta1 = "未装完";
                BLLFactory.CreateWorkBLL().UpdateSta(num, sta1);
                Response.Redirect(string.Format("DXC.aspx?Num={0}", num));
            }



        }
        
        
        
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        if (txtGood.Text.ToString() != "")
            good = Convert.ToInt32(txtGood.Text.ToString());
        else
            Response.Write(" <script   language=javascript> window.alert( ' 请填入实际好车数！ ');</script> ");
        if (txtBad.Text.ToString() != "")
            bad = Convert.ToInt32(txtBad.Text.ToString());
        else
            Response.Write(" <script   language=javascript> window.alert( ' 请填入实际坏车数！ ');</script> ");
        if (txtCc.Text.ToString() != "")
            cc = Convert.ToInt32(txtCc.Text.ToString());
        else
            Response.Write(" <script   language=javascript> window.alert( ' 请填入实际敞车数！ ');</script> ");
        if (txtPb.Text.ToString() != "")
            pb = Convert.ToInt32(txtPb.Text.ToString());
        else
            Response.Write(" <script   language=javascript> window.alert( ' 请填入实际平板数！ ');</script> ");
        if (txtQH.Text.ToString() != "")
            qh = Convert.ToInt32(txtQH.Text.ToString());
        else
            Response.Write(" <script   language=javascript> window.alert( ' 请填入实际期货车数！ ');</script> ");
        if (good != (cc + pb + qh))
            Response.Write(" <script   language=javascript> window.alert( ' 请填入好车数、平板数、敞车数、期货车数填写不正确！ ');</script> ");
        else
        {
            int f = BLLFactory.CreateWorkBLL().UpdatePD(num, txtBanbie.Text.ToString(), txtBanzu.Text.ToString(), Convert.ToDecimal(txtDun.Text.ToString()), txtNeirong.Text.ToString(), good, bad, txtPos.Text.ToString(), pb, cc, qh, txtPDtime.Text.ToString());
            if(f >= 1)
                Response.Write(" <script   language=javascript> window.alert( ' 修改成功！ ');</script> ");
        }
        Response.Redirect(string.Format("WORK.aspx?Sta={0}", "配到"));
        
    }
}
